package Arrays101.Day02;

import java.util.Arrays;

public class Day02_2 {
    public static void main(String[] args) {
        merge(new int[]{-1,0,3,0,0,0},3,new int[]{},1);
    }

    /**
     * Merge Sorted Array
     *
     * Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
     * The number of elements initialized in nums1 and nums2 are m and n respectively.
     * You may assume that nums1 has a size equal to m + n such that it has enough space to hold additional elements from nums2.
     * @param nums1
     * @param m
     * @param nums2
     * @param n
     */
    public static void merge(int[] nums1, int m, int[] nums2, int n){

        int[] nums3 = new int[nums1.length + nums2.length];
        System.arraycopy(nums1,0,nums3,0,nums1.length);
        System.arraycopy(nums2,0,nums3,nums1.length,nums2.length);
        int index=0;
        int[]nums4=new int[nums1.length];

        for (int i = 0; i < nums3.length; i++) {
            if (nums3[i]!=0){
                nums4[index++]=nums3[i];
            }
        }
        for (int i = 0; i < nums1.length; i++) {
            nums1[i]=nums4[i];
        }
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums1.length-i-1; j++) {

                if (nums1[j]>nums1[j+1]){
                    int b=0;
                    b=nums1[j];
                    nums1[j]=nums1[j+1];
                    nums1[j+1]=b;
                }
            }
        }


        for (int i = 0; i < nums1.length; i++) {
            System.out.print(nums1[i]);
        }

    }
}
/*
 public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int tail1 = m - 1, tail2 = n - 1, finished = m + n - 1;
        while (tail1 >= 0 && tail2 >= 0) {
            nums1[finished--] = (nums1[tail1] > nums2[tail2]) ?
                    nums1[tail1--] : nums2[tail2--];
        }

        while (tail2 >= 0) { //only need to combine with remaining nums2
            nums1[finished--] = nums2[tail2--];
        }
    }
 */
